﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using Terraria.Localization;
using Terraria.ModLoader.IO;
using Terraria.UI;

namespace Terraria.ModLoader.UI
{
	// Token: 0x02000289 RID: 649
	internal class UIExtractMod : UIState
	{
		// Token: 0x06001A5A RID: 6746 RVA: 0x00428ED4 File Offset: 0x004270D4
		public override void OnInitialize()
		{
			this.loadProgress = new UILoadProgress();
			this.loadProgress.Width.Set(0f, 0.8f);
			this.loadProgress.MaxWidth.Set(600f, 0f);
			this.loadProgress.Height.Set(150f, 0f);
			this.loadProgress.HAlign = 0.5f;
			this.loadProgress.VAlign = 0.5f;
			this.loadProgress.Top.Set(10f, 0f);
			base.Append(this.loadProgress);
		}

		// Token: 0x06001A5B RID: 6747 RVA: 0x00428F80 File Offset: 0x00427180
		public override void OnActivate()
		{
			Main.menuMode = 10019;
			Task.Factory.StartNew<Exception>(() => Interface.extractMod._Extract()).ContinueWith(delegate(Task<Exception> t)
			{
				Exception result = t.Result;
				if (result != null)
				{
					ErrorLogger.LogException(result, Language.GetTextValue("tModLoader.ExtractErrorWhileExtractingMod", this.mod.Name));
					return;
				}
				Main.menuMode = this.gotoMenu;
			}, TaskScheduler.FromCurrentSynchronizationContext());
		}

		// Token: 0x06001A5C RID: 6748 RVA: 0x00012CC5 File Offset: 0x00010EC5
		internal void SetMod(LocalMod mod)
		{
			this.mod = mod;
		}

		// Token: 0x06001A5D RID: 6749 RVA: 0x00012CCE File Offset: 0x00010ECE
		internal void SetGotoMenu(int gotoMenu)
		{
			this.gotoMenu = gotoMenu;
		}

		// Token: 0x06001A5E RID: 6750 RVA: 0x00428FD8 File Offset: 0x004271D8
		private Exception _Extract()
		{
			UIExtractMod.<>c__DisplayClass8_0 <>c__DisplayClass8_ = new UIExtractMod.<>c__DisplayClass8_0();
			<>c__DisplayClass8_.<>4__this = this;
			<>c__DisplayClass8_.log = null;
			try
			{
				UIExtractMod.<>c__DisplayClass8_1 <>c__DisplayClass8_2 = new UIExtractMod.<>c__DisplayClass8_1();
				<>c__DisplayClass8_2.CS$<>8__locals1 = <>c__DisplayClass8_;
				<>c__DisplayClass8_2.dir = Path.Combine(Main.SavePath, "Mod Reader", this.mod.Name);
				if (Directory.Exists(<>c__DisplayClass8_2.dir))
				{
					Directory.Delete(<>c__DisplayClass8_2.dir, true);
				}
				Directory.CreateDirectory(<>c__DisplayClass8_2.dir);
				<>c__DisplayClass8_2.CS$<>8__locals1.log = new StreamWriter(Path.Combine(<>c__DisplayClass8_2.dir, "tModReader.txt"))
				{
					AutoFlush = true
				};
				if (this.mod.properties.hideCode)
				{
					<>c__DisplayClass8_2.CS$<>8__locals1.log.WriteLine(Language.GetTextValue("tModLoader.ExtractHideCodeMessage"));
				}
				else if (!this.mod.properties.includeSource)
				{
					<>c__DisplayClass8_2.CS$<>8__locals1.log.WriteLine(Language.GetTextValue("tModLoader.ExtractNoSourceCodeMessage"));
				}
				if (this.mod.properties.hideResources)
				{
					<>c__DisplayClass8_2.CS$<>8__locals1.log.WriteLine(Language.GetTextValue("tModLoader.ExtractHideResourcesMessage"));
				}
				<>c__DisplayClass8_2.CS$<>8__locals1.log.WriteLine(Language.GetTextValue("tModLoader.ExtractFileListing"));
				<>c__DisplayClass8_2.i = 0;
				this.mod.modFile.Read(TmodFile.LoadedState.Streaming, delegate(string name, int len, BinaryReader reader)
				{
					<>c__DisplayClass8_2.<_Extract>g__WriteFile|0(name, reader.ReadBytes(len));
				});
				foreach (KeyValuePair<string, byte[]> keyValuePair in this.mod.modFile)
				{
					<>c__DisplayClass8_2.<_Extract>g__WriteFile|0(keyValuePair.Key, keyValuePair.Value);
				}
			}
			catch (Exception ex)
			{
				StreamWriter log = <>c__DisplayClass8_.log;
				if (log != null)
				{
					log.WriteLine(ex);
				}
				return ex;
			}
			finally
			{
				StreamWriter log2 = <>c__DisplayClass8_.log;
				if (log2 != null)
				{
					log2.Close();
				}
				LocalMod localMod = this.mod;
				if (localMod != null)
				{
					localMod.modFile.UnloadAssets();
				}
			}
			return null;
		}

		// Token: 0x04001796 RID: 6038
		private UILoadProgress loadProgress;

		// Token: 0x04001797 RID: 6039
		private int gotoMenu;

		// Token: 0x04001798 RID: 6040
		private LocalMod mod;

		// Token: 0x04001799 RID: 6041
		private static IList<string> codeExtensions = new List<string>(ModCompile.sourceExtensions)
		{
			".dll",
			".pdb"
		};
	}
}
